﻿using ACC.Business;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using ACC.Device.PLC.Siemens;
using ACC.LineDevice.KukaPack;
using System;
using System.Text;

namespace Script
{
    /// <summary>
    /// 上盖下线
    /// </summary>
    class L1P10061 : StnProcessKukaPack
    {
        public L1P10061(StnRunningData data)
            : base(data)
        {
            //materialTypeCount = 1;
            _data.ProcessUnitIDWhenLoad = false;
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 7000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 7066;

                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 7034;
                kukaSiemensDev.DB193_ACC_Offset = 7068;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 7034;
                kukaSiemensDev.DB194_PLC_Data_Offset = 7078;

                kukaSiemensDev.BarcodeLength = 30;
                kukaSiemensDev.LoadBarcodeCount = 2;
                kukaSiemensDev.UnloadBarcodeCount = 2;
            }

            return dev;

        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            ACCCode rs = base.ReadLoadParam(dev, out param);
            _data.Components = new BomItemCfg[] { };
            param.ComponentSN[0] = "";
            return rs;
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            _data.Components = new BomItemCfg[] { _data.PartNo.BomCfg.GetBomItemCfgByIDName("上箱体") };
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            // TO-DO: _data.LastLoadUnit.UnitSN 为什么? must change
            param.UnitSN = _data.LastLoadUnit.UnitSN;
            param.ComponentSN[0] = GetUnLoadPallet();
            return rs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetUnLoadPallet()
        {
            string _pallet = "";
            try
            {
                byte[] data = new byte[32];
                byte[] temp = new byte[30];
                ((SiemensDevice)_Device).ReadDBBytes(((SiemensDevice)_Device).DB194, kukaSiemensDev.DB194_PLC_Offset + 38, data);
                Array.Copy(data, 2, temp, 0, temp.Length);
                _pallet = Encoding.ASCII.GetString(temp);
                if (_pallet != null && _pallet.Length > data[1])
                {
                    _pallet = _pallet.Substring(0, data[1]);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
            }
            return _pallet;
        }
    }
}
